/**
  EG4Sim project, Anyang Normal University && IMP-CAS
  \class EG4DCTaD
  \brief the model of the DC downstream of the target
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2023-11-08
  \date 2023-11-08 last modified
  \copyright Copyright (c) 2023-2024 IMP-CAS with LGPLv3 LICENSE
*/

#include "EG4DCTaD.h"
#include "G4Box.hh"
#include "G4Material.hh"
#include "G4LogicalVolume.hh"
#include "G4VisAttributes.hh"
#include "G4PVPlacement.hh"
#include "G4Colour.hh"
#include "G4SystemOfUnits.hh"

EG4DCTaD::EG4DCTaD() : EG4Det(){
  G4Material *dcgas = G4Material::GetMaterial("c20Lanzhou", true);
  G4Material *Al5052 = G4Material::GetMaterial("Al5052", true);

  fLV = new G4LogicalVolume(new G4Box("DCTaD", 175.*mm, 175.*mm, 44.*mm), dcgas, "DCTaD");
  SetColor(0., 1., 0.); // green
  // add the insensitive supporting frame wrapping the sense wires //
  auto frmX = new G4LogicalVolume(new G4Box("frm_DCTaD_X", 37.5*mm, 175.*mm, 1.*mm), Al5052, "frm_DCTaD_X");
  auto frmY = new G4LogicalVolume(new G4Box("frm_DCTaD_Y", 100.*mm, 37.5*mm, 1.*mm), Al5052, "frm_DCTaD_Y");
  for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++){
    const int id = i*2 + j;
    new G4PVPlacement(nullptr, G4ThreeVector((1.-2.*i)*137.5*mm, 0., (1.-2.*j)*43.*mm),
      frmX, "frm_DCTaD_X", fLV, false, id, true);
    new G4PVPlacement(nullptr, G4ThreeVector(0., (1.-2.*i)*137.5*mm, (1.-2.*j)*43.*mm),
      frmY, "frm_DCTaD_Y", fLV, false, id, true);
  } // end for over i and j
  auto frmZY = new G4LogicalVolume(new G4Box("frm_DCTaD_ZY", 175.*mm, 1.*mm, 42.*mm), Al5052, "frm_DCTaD_ZY");
  auto frmZX = new G4LogicalVolume(new G4Box("frm_DCTaD_ZX", 1.*mm, 173.*mm, 42.*mm), Al5052, "frm_DCTaD_ZX");
  for(int i = 0; i < 2; i++){
    new G4PVPlacement(nullptr, G4ThreeVector(0., (1.-2.*i)*174.*mm, 0.), frmZY,
      "frm_DCTaD_ZY", fLV, false, i, true);
    new G4PVPlacement(nullptr, G4ThreeVector((1.-2.*i)*174.*mm, 0., 0.), frmZX,
      "frm_DCTaD_ZX", fLV, false, i, true);
  } // end for over i

  // wire planes
  fWLV = new G4LogicalVolume(new G4Box("wireDCTaD", 100.*mm, 100.*mm, 5.*mm),
    dcgas, "WireDCTaD");
  fWLV->SetVisAttributes(new G4VisAttributes(G4Colour(1., 1., 0.))); // yellow

  // due to historical reasons
  // x0 plane's z is the DC's center from laser survey
  const double z0 = 0.; // -17.5*mm;
  fWX[0] = new G4PVPlacement(0, G4ThreeVector(0., 0., 17.5*mm + z0),
    fWLV, "WireDCTaX0", fLV, false, 0, true);
  fWX[1] = new G4PVPlacement(0, G4ThreeVector(0., 0., 7.5*mm + z0),
    fWLV, "WireDCTaX1", fLV, false, 1, true);
  fWY[0] = new G4PVPlacement(0, G4ThreeVector(0., 0., -7.5*mm + z0),
    fWLV, "WireDCTaY0", fLV, false, 2, true);
  fWY[1] = new G4PVPlacement(0, G4ThreeVector(0., 0., -17.5*mm + z0),
    fWLV, "WireDCTaY1", fLV, false, 3, true);
} // end ctor

EG4DCTaD::~EG4DCTaD(){}
